import shutil


def copy_all(table_name):
    source_path = "D:/temp/generate-code/"

    intf_desc = "E:/work/80-mcs/backends/interface-dao/src/main/java/"
    intf_files = {
        "com/micode/mcs/interfaces/dao/to/": table_name + "TO.java",
        # "com/micode/mcs/interfaces/dao/query/": table_name + "Query.java",
        # "com/micode/mcs/interfaces/dao/dto/": table_name + "DTO.java",
        # "com/micode/mcs/interfaces/dao/": table_name + "DaoApi.java",
    }

    dao_desc = "E:/work/80-mcs/backends/dao-svr/src/main/java/"
    dao_files = {
        "com/micode/mcs/daos/entity/": table_name + "DO.java",
        "com/micode/mcs/daos/dao/": table_name + "Dao.java",
        "com/micode/mcs/daos/service/": table_name + "Service.java",
        "com/micode/mcs/daos/service/impl/": table_name + "ServiceImpl.java",
        "com/micode/mcs/daos/controller/": table_name + "DaoController.java",
    }

    mapper_desc = "E:/work/80-mcs/backends/dao-svr/src/main/resources/"
    mapper_files = {
        "mapper/": table_name + "Mapper.xml",
    }

    copy_by_names(source_path, intf_desc, intf_files)
    # copy_by_names(source_path, dao_desc, dao_files)
    copy_by_names(source_path, mapper_desc, mapper_files)


def copy_by_names(source_path, desc_path, files):
    for filepath in files.keys():
        filename = files.get(filepath)
        source_file = source_path + filepath + filename
        desc_file = desc_path + filepath + filename
        copy_one(source_file, desc_file)


def copy_one(source, target):
    # adding exception handling
    try:
        shutil.copy(source, target)
    except IOError as e:
        print("Unable to copy file. %s" % e)


if __name__ == "__main__":
    # 输入文件名称
    the_name = input("Please input table name：")

    if the_name:
        copy_all(the_name)
